﻿using Lufei_CaterBLL;
using Lufei_CaterModel;
using Lufei_CaterUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ItcastCater
{
    public partial class FrmCategory : Form
    {

        public FrmCategory()
        {
            InitializeComponent();
        }

        CategoryBLL bll = new CategoryBLL();
        ProductBLL bllpro= new ProductBLL();

        /// <summary>
        /// 获取所有商品信息
        /// </summary>
        private void InitialProduct()
        {
            List<ProductInfo> list= bllpro.GetProducts();
            dgvProductInfo.AutoGenerateColumns = false;//取消自动生成列
            dgvProductInfo.DataSource = list;
            dgvProductInfo.SelectedRows[0].Selected = false;
        }

   
        private void FrmCategory_Load(object sender, EventArgs e)
        {
            InitialCategory();
            InitialProduct();
        }

        /// <summary>
        /// 获取所有商品类别
        /// </summary>
        private void InitialCategory()
        {
            dgvCategoryInfo.AutoGenerateColumns = false;//取消自动生成列
             List<CategoryInfo> list=  bll.GetCategoryInfos();
            //加载所有类别的数据
            dgvCategoryInfo.DataSource = list;
            dgvCategoryInfo.SelectedRows[0].Selected = false;


            //给下拉框做数据绑定
            cmbCategory.DisplayMember = "catName";
            cmbCategory.ValueMember = "catid";

            cmbCategory.DataSource= list;
        }


        //添加商品
        private void btnAddCategory_Click(object sender, EventArgs e)
        {

            LoadChangeCategory(1);
        }

        //修改商品
        private void btnUpdateCategory_Click(object sender, EventArgs e)
        {
            //如果选择了行数据 ，在执行代码 否之直接return
            if (dgvCategoryInfo.SelectedRows.Count > 0)
            {
                LoadChangeCategory(2);
            }
            else
            {
                MessageBox.Show("请选择数据");
                return;
            }
        }


        //1.创建的是同一个窗体 a bnew一个窗体
        FrmChangeCategory frmChangeCategory=new FrmChangeCategory();
        //2.定义事件
        private EventHandler My_EventHandler;

        public void LoadChangeCategory(int type)
        {
            if (type==1)
            {
                frmChangeCategory.Text = "添加商品类别数据";
                //插入数据
            }
            else if (type==2)
            {
                frmChangeCategory.Text = "修改商品类别数据";
                //修改数据
                //1.1获取要修改的数据----》关系转对象
                //2.1定义事件---》注册事件--》调用事件----》把对象传递过去(参数)
                this.My_EventHandler += frmChangeCategory.SetValue;
                UpdateMemmberInfo<CategoryInfo> updateMemmber = new UpdateMemmberInfo<CategoryInfo>();
                //记录当前操作的类型
                updateMemmber.Type = 2;
                //获取选择行数据的ID
                int id = Convert.ToInt32(dgvCategoryInfo.SelectedRows[0].Cells[0].Value.ToString());

                 CategoryInfo ci=   bll.GetCategoryInfoByID(id);
                //把对象赋值给updateCategoryInfo的ibj属性
                updateMemmber.obj = ci;
                if (this.My_EventHandler!=null)
                {
                    this.My_EventHandler(this,updateMemmber);
                }
            }

            frmChangeCategory.FormClosed += FrmChangeCategory_FormClosed; 
            frmChangeCategory.Show();
        }


        private void FrmChangeCategory_FormClosed(object sender, FormClosedEventArgs e)
        {
            InitialCategory();
        }

        //删除
        private void benDeleteCategory_Click(object sender, EventArgs e)
        {
            //判断是否选中这一行
            if (dgvCategoryInfo.SelectedRows.Count>0)
            {
                //1.判断选择行的商品类别下面有没有商品 

                //1.1如果有就不能删 mb
                //1.2没有---》软删除
                //获取id
                int id = Convert.ToInt32(dgvCategoryInfo.SelectedRows[0].Cells[0].Value.ToString());
                int res=  bll.GetProductsByCatoryId(id);
                if (res>0)
                {
                    //有数据
                    MessageBox.Show("当前选择的类别下有数据，不允许删除");
                }
                else
                {
                    string msg= bll.SoftDeleteById(id)?"删除成功":"删除是";
                    MessageBox.Show(msg);
                    InitialCategory();
                }
            }
            else
            {
                MessageBox.Show("请选择一行进行删除");
                return;
            }
        }

        /// <summary>
        /// 文本框的选择发生了改变  根据用户在文本框输入的内如 重新加载dgv中的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            string str=txtSearch.Text.Trim();
            if (string.IsNullOrEmpty(str))
            {
                //显示全部
                InitialProduct();
            }
            else
            {
                //判断拼音还是数字
                int temp = 0;
                if (char.IsLetter(str[0]))
                {
                    //拼音
                    temp = 1;
                 dgvProductInfo.DataSource=    bllpro.GetProductInfoBySpellOrNum(str,temp);
                }
                else
                {
                    temp = 2;
                    dgvProductInfo.DataSource = bllpro.GetProductInfoBySpellOrNum(str, temp);
                }
            }
        }

        //选择改变的时候触发-----检索
        private void cmbCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            //SelectedValue ---- valuemember  catid
            int id = Convert.ToInt32(cmbCategory.SelectedValue);
            List<ProductInfo> list = bllpro.GetProductInfoByID(id);
            dgvProductInfo.DataSource = list;
        }

        //添加
        //添加
        private void btnAddPro_Click(object sender, EventArgs e)
        {
            FrmAddProduct frmAddProduct = new FrmAddProduct();
            frmAddProduct.FormClosed += FrmAddProduct_FormClosed;     
            frmAddProduct.Show();

        }

        private void FrmAddProduct_FormClosed(object sender, FormClosedEventArgs e)
        {
            InitialProduct();
        }


        //2.定义事件
        private EventHandler My_EventHandlerUp;


        //修改
        private void btnUpdatePro_Click(object sender, EventArgs e)
        {
            if (dgvProductInfo.SelectedRows.Count>0)
            {
                FrmChangeProduct frmChange = new FrmChangeProduct();

                frmChange.FormClosed += FrmChange_FormClosed;
                UpdateMemmberInfo<ProductInfo> update = new UpdateMemmberInfo<ProductInfo>();

                this.My_EventHandlerUp += frmChange.SetValue;
                //获取选择行的id
                int id = Convert.ToInt32(dgvProductInfo.SelectedRows[0].Cells[0].Value.ToString());
                //根据id去查询数据
                ProductBLL bll = new ProductBLL();
                ProductInfo pro = bll.GetProductInfoOneByID(id);

                update.obj = pro;


                //调用事件
                if (this.My_EventHandlerUp != null)
                {
                    this.My_EventHandlerUp(this, update);
                }
                frmChange.Show();
            }
            else
            {
                MessageBox.Show("请选择数据");
                return;
            }
        }

        private void FrmChange_FormClosed(object sender, FormClosedEventArgs e)
        {
            InitialProduct();
        }

        private void btnDeletePro_Click(object sender, EventArgs e)
        {
          DialogResult result=  MessageBox.Show("您是否选择删除数据","提示",MessageBoxButtons.YesNo);
            if (result==DialogResult.Yes)
            {
                //删除代码
                ProductBLL bll = new ProductBLL();
              int id=Convert.ToInt32(dgvProductInfo.SelectedRows[0].Cells[0].Value);
               bool isDelete= bll.ProductDelectById(id);
                if (isDelete)
                {
                    MessageBox.Show("删除成功");
                    InitialProduct();
                }
                else
                {
                    MessageBox.Show("删除失败");
                    InitialProduct();
                }
            }
            else if (result==DialogResult.No)
            {
                MessageBox.Show("取消成功");
            }
        }
    }
}
